package com.shycloud.mido.common.pay.alipay.utils;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.shycloud.mido.common.pay.alipay.AlipayConfig;
import com.shycloud.mido.common.pay.alipay.bean.AliReturnPayBean;
import com.shycloud.mido.common.pay.alipay.bean.OrderBean;
import com.shycloud.mido.common.pay.wxpay.WXPay;
import java.time.LocalDate;
import lombok.SneakyThrows;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Description:支付宝支付接口 Date:2019/04/23 15:58
 */
public class AliPayUtil {

	private static Log logger = LogFactory.getLog(WXPay.class);

	private static String AGREEMENTNO = "12345678912345678912345678912351";

	private static String ORDERNO = "12345678912345678912345678912390";

	/**
	 * 支付宝支付
	 *
	 * @param orderBean
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/8/7 10:22
	 */
	public static String alipay(OrderBean orderBean) {

		//获得初始化的AlipayClient
		//实例化客户端（参数：网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型），为了取得预付订单信息
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
		//设置请求参数
		//支付宝App支付接口
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
		//设置页面跳转同步通知页面路径
		alipayRequest.setReturnUrl(orderBean.getReturnUrl());
		//设置回调地址，服务器异步通知页面路径
		alipayRequest.setNotifyUrl(orderBean.getNotifyUrl());
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(orderBean.getBody());
		//商品名称
		model.setSubject(orderBean.getSubjecy());
		//订单编号
		model.setOutTradeNo(orderBean.getOutTradeNo());
		//超时时间
		model.setTimeoutExpress("30m");
		//支付金额
		model.setTotalAmount(orderBean.getTotalAmount());

		model.setAgreementSignParams(orderBean.getSignParams());
		if (orderBean.getSignParams() != null) {
			model.setProductCode("CYCLE_PAY_AUTH");
		} else {
			model.setProductCode("QUICK_MSECURITY_PAY");
		}

		alipayRequest.setBizModel(model);
		try {
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest);
			if (response.isSuccess()) {
				logger.info("调用成功");
			} else {
				response.setMsg("error");
				logger.info("调用失败");
			}
			logger.info("*********************\n返回结果为：" + response.getBody());
			return response.getBody();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 支付宝支付
	 *
	 * @param orderBean
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/8/7 10:22
	 */
	public static String alipayH5(OrderBean orderBean) {

		//获得初始化的AlipayClient
		//实例化客户端（参数：网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型），为了取得预付订单信息
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.ALI_GAIN_SHARING_APPID,
				AlipayConfig.ALI_GAIN_SHARING_RSA2_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY, AlipayConfig.SIGNTYPE_RSA2);
		//设置请求参数
		//支付宝App支付接口
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
		//设置页面跳转同步通知页面路径
		alipayRequest.setReturnUrl(orderBean.getReturnUrl());
		//设置回调地址，服务器异步通知页面路径
		alipayRequest.setNotifyUrl(orderBean.getNotifyUrl());
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(orderBean.getBody());
		//商品名称
		model.setSubject(orderBean.getSubjecy());
		//订单编号
		model.setOutTradeNo(orderBean.getOutTradeNo());
		//超时时间
		model.setTimeoutExpress("30m");
		//支付金额
		model.setTotalAmount(orderBean.getTotalAmount());
		model.setProductCode("QUICK_MSECURITY_PAY");
		alipayRequest.setBizModel(model);
		alipayRequest.setReturnUrl(orderBean.getReturnUrl());
		try {
			AlipayTradeWapPayResponse execute = alipayClient.pageExecute(alipayRequest);
			if (execute.isSuccess()) {
				logger.info("调用成功");
			} else {
				execute.setMsg("error");
				logger.info("调用失败");
			}
			logger.info("*********************\n返回结果为：" + execute.getBody());
			return execute.getBody();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 支付宝退款接口
	 *
	 * @param orderBean 商品信息
	 * @return
	 */
	public static String aliRefund(OrderBean orderBean) {
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
		//设置请求参数
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
		//AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		//设置页面跳转同步通知页面路径
		alipayRequest.setReturnUrl(orderBean.getReturnUrl());
		//设置回调地址，服务器异步通知页面路径
		alipayRequest.setNotifyUrl(orderBean.getNotifyUrl());
		try {
			alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderBean.getOutTradeNo() + "\","
					+ "\"trade_no\":\"" + orderBean.getTradeNo() + "\","
					+ "\"refund_amount\":\"" + orderBean.getRefundAmount() + "\","
					+ "\"refund_reason\":\"" + orderBean.getRefundReason() + "\","
					+ "\"out_request_no\":\"" + orderBean.getOutRequestNo() + "\"}");
			//请求
			String result;

			//请求
			result = alipayClient.execute(alipayRequest).getBody();
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 支付宝的验签方法
	 *
	 * @param req
	 * @return
	 */
	public static boolean checkSign(HttpServletRequest req) {
		Map<String, String[]> requestMap = req.getParameterMap();
		Map<String, String> paramsMap = new HashMap<>();
		requestMap.forEach((key, values) -> {
			String strs = "";
			for (String value : values) {
				strs = strs + value;
			}
			logger.info(("key值为" + key + "value为：" + strs));
			paramsMap.put(key, strs);
		});
		//调用SDK验证签名
		try {
			return AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info("*********************验签失败********************");
			return false;
		}
	}

	/**
	 * 支付宝的验签方法
	 *
	 * @param req
	 * @return
	 */
	public static boolean checkSignH5(HttpServletRequest req) {
		Map<String, String[]> requestMap = req.getParameterMap();
		Map<String, String> paramsMap = new HashMap<>();
		requestMap.forEach((key, values) -> {
			String strs = "";
			for (String value : values) {
				strs = strs + value;
			}
			logger.info(("key值为" + key + "value为：" + strs));
			paramsMap.put(key, strs);
		});
		//调用SDK验证签名
		try {
			return AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY, AlipayConfig.CHARSET,
					AlipayConfig.SIGNTYPE_RSA2);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info("*********************验签失败********************");
			return false;
		}
	}

	/**
	 * 支付宝回调的接口
	 *
	 * @param returnPay 支付宝返回数据用实体bean接收
	 * @return boolean
	 * @throws IOException
	 */
	public static boolean returnPay(HttpServletRequest req, AliReturnPayBean returnPay, HttpServletResponse response)
			throws IOException {
		boolean flag = false;
		String tradeSuccess = "TRADE_SUCCESS";
		if (!AliPayUtil.checkSign(req)) {
			return flag;
		}
		if (returnPay == null) {
			logger.info("支付宝的returnPay返回为空");
			flag = true;
			return flag;
		}
		logger.info("支付宝的returnPay" + returnPay.toString());
		//成功返回 TRADE_SUCCESS
		if (tradeSuccess.equals(returnPay.getTrade_status())) {
			flag = true;
			logger.info("支付宝的支付状态为TRADE_SUCCESS");
		}
		return flag;
	}


	public static boolean returnPayH5(HttpServletRequest req, AliReturnPayBean returnPay, HttpServletResponse response) {
		boolean flag = false;
		String tradeSuccess = "TRADE_SUCCESS";
		if (!AliPayUtil.checkSignH5(req)) {
			return flag;
		}
		if (returnPay == null) {
			logger.info("支付宝的returnPay返回为空");
			flag = true;
			return flag;
		}
		logger.info("支付宝的returnPay" + returnPay.toString());
		//成功返回 TRADE_SUCCESS
		if (tradeSuccess.equals(returnPay.getTrade_status())) {
			flag = true;
			logger.info("支付宝的支付状态为TRADE_SUCCESS");
		}
		return flag;
	}

	/**
	 * 分账关系绑定
	 *
	 * @param account
	 * @param name
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/11/13 14:06
	 **/
	@SneakyThrows
	public static boolean AliRelationBind(String account, String name, String orderNo) {

		AlipayClient alipayClient = new DefaultAlipayClient(
				AlipayConfig.URL, AlipayConfig.ALI_GAIN_SHARING_APPID,
				AlipayConfig.ALI_GAIN_SHARING_RSA2_PRIVATE_KEY, AlipayConfig.FORMAT,
				AlipayConfig.CHARSET, AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY,
				AlipayConfig.SIGNTYPE_RSA2
		);

		//分账关系绑定接口请求
		AlipayTradeRoyaltyRelationBindRequest request = new AlipayTradeRoyaltyRelationBindRequest();

		//============================================创建请求参数===START==============================================//
		Map<String, Object> requestMap = new HashMap<>();
		List<RoyaltyEntity> bindUser = new ArrayList<>();
		RoyaltyEntity aliRelationBindBean = new RoyaltyEntity();
		//分帐方类型：支付宝登录号
		aliRelationBindBean.setType("loginName");
		//分账方帐号：支付宝登录号
		aliRelationBindBean.setAccount(account);
		//支付宝实名认证姓名
		aliRelationBindBean.setName(name);
		//描述
		aliRelationBindBean.setMemo("分账给测试商户");

		bindUser.add(aliRelationBindBean);

		requestMap.put("receiver_list", bindUser);
		requestMap.put("out_request_no", orderNo);
		String json = JSON.toJSONString(requestMap);
		request.setBizContent(json);
		//============================================创建请求参数==E N D===============================================//

		//调用分账关系绑定接口
		AlipayTradeRoyaltyRelationBindResponse response = alipayClient.execute(request);

		return response.isSuccess();
	}

	/**
	 * 分账关系解绑
	 *
	 * @param account
	 * @param name
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/11/13 14:06
	 **/
	@SneakyThrows
	public static boolean AliRelationUnBind(String account, String name, String orderNo) {

		AlipayClient alipayClient = new DefaultAlipayClient(
				AlipayConfig.URL, AlipayConfig.ALI_GAIN_SHARING_APPID,
				AlipayConfig.ALI_GAIN_SHARING_RSA2_PRIVATE_KEY, AlipayConfig.FORMAT,
				AlipayConfig.CHARSET, AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY,
				AlipayConfig.SIGNTYPE_RSA2
		);

		//分账关系绑定接口请求
		AlipayTradeRoyaltyRelationUnbindRequest request = new AlipayTradeRoyaltyRelationUnbindRequest();

		//============================================创建请求参数===START==============================================//
		Map<String, Object> requestMap = new HashMap<>();
		List<RoyaltyEntity> bindUser = new ArrayList<>();
		RoyaltyEntity royaltyEntity = new RoyaltyEntity();
		//分帐方类型：支付宝登录号
		royaltyEntity.setType("loginName");
		//分账方帐号：支付宝登录号
		royaltyEntity.setAccount(account);
		//支付宝实名认证姓名
		royaltyEntity.setName(name);
		//描述
		royaltyEntity.setMemo("分账给测试商户");

		bindUser.add(royaltyEntity);

		requestMap.put("receiver_list", bindUser);
		requestMap.put("out_request_no", orderNo);
		String json = JSON.toJSONString(requestMap);
		request.setBizContent(json);
		//============================================创建请求参数==E N D===============================================//
		//调用分账关系绑定接口
		AlipayTradeRoyaltyRelationUnbindResponse response = alipayClient.execute(request);

		return response.isSuccess();
	}

	/**
	 * 查询分账关系
	 *
	 * @param pageNum
	 * @param pageSize
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/11/13 15:46
	 **/
	@SneakyThrows
	public static boolean AliRelationSearch(Integer pageNum, Integer pageSize, String orderNo) {

		AlipayClient alipayClient = new DefaultAlipayClient(
				AlipayConfig.URL, AlipayConfig.ALI_GAIN_SHARING_APPID,
				AlipayConfig.ALI_GAIN_SHARING_RSA2_PRIVATE_KEY, AlipayConfig.FORMAT,
				AlipayConfig.CHARSET, AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY,
				AlipayConfig.SIGNTYPE_RSA2
		);

		//查询分账关系接口请求
		AlipayTradeRoyaltyRelationBatchqueryRequest request = new AlipayTradeRoyaltyRelationBatchqueryRequest();

		Map<String, Object> requestMap = MapUtil.newHashMap(3);
		requestMap.put("page_num", pageNum);
		requestMap.put("page_size", pageSize);
		requestMap.put("out_request_no", orderNo);
		String json = JSON.toJSONString(requestMap);
		request.setBizContent(json);

		AlipayTradeRoyaltyRelationBatchqueryResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {

			JSONObject object = JSONObject.parseObject(response.getBody());
			String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
					SerializerFeature.WriteDateUseDateFormat);
			System.out.println(pretty);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 分账接口
	 *
	 * @param phone      分帐方支付宝账号
	 * @param orderNo    订单编号
	 * @param aliOrderNo 支付宝返回流水号
	 * @param amount     分账金额
	 * @param desc       描述
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/11/16 17:27
	 **/
	@SneakyThrows
	public static boolean aliRelationSettle(String phone, String orderNo, String aliOrderNo, String amount, String desc) {

		AlipayClient alipayClient = new DefaultAlipayClient(
				AlipayConfig.URL, AlipayConfig.ALI_GAIN_SHARING_APPID,
				AlipayConfig.ALI_GAIN_SHARING_RSA2_PRIVATE_KEY, AlipayConfig.FORMAT,
				AlipayConfig.CHARSET, AlipayConfig.ALI_GAIN_SHARING_ALI_PUBLIC_KEY,
				AlipayConfig.SIGNTYPE_RSA2
		);

		AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();

		RoyaltyInfo royaltyInfo = new RoyaltyInfo();
		//分账类型：普通分账类型
		royaltyInfo.setRoyaltyType("ROYALTY");

		AlipayTradeOrderSettleModel alipayTradeOrderSettleModel = new AlipayTradeOrderSettleModel();

		// 结算请求流水号 开发者自行生成并保证唯一性
		alipayTradeOrderSettleModel.setOutRequestNo(orderNo);
		// 支付宝订单号
		alipayTradeOrderSettleModel.setTradeNo(aliOrderNo);
		// 操作员ID
		alipayTradeOrderSettleModel.setOperatorId("A0001");

		//分账明细信息
		List<OpenApiRoyaltyDetailInfoPojo> royaltyParameters = new ArrayList<>();
		OpenApiRoyaltyDetailInfoPojo openApiRoyaltyDetailInfoPojo = new OpenApiRoyaltyDetailInfoPojo();

		//分账的金额
		openApiRoyaltyDetailInfoPojo.setAmount(amount);
		//分账描述
		openApiRoyaltyDetailInfoPojo.setDesc(desc);
		//分账类型. 普通分账为：transfer; 补差为：replenish; 为空默认为分账transfer;
		//openApiRoyaltyDetailInfoPojo.setRoyaltyType();
		//收入方账户类型
		openApiRoyaltyDetailInfoPojo.setTransInType("loginName");
		//收入方账户
		openApiRoyaltyDetailInfoPojo.setTransIn(phone);
		//支出方账户类型
		openApiRoyaltyDetailInfoPojo.setTransOutType("userId");
		//支出方账户
		openApiRoyaltyDetailInfoPojo.setTransOut("2088431761028680");

		royaltyParameters.add(openApiRoyaltyDetailInfoPojo);
		//分账明细信息
		alipayTradeOrderSettleModel.setRoyaltyParameters(royaltyParameters);

		request.setBizModel(alipayTradeOrderSettleModel);

		AlipayTradeOrderSettleResponse response = alipayClient.execute(request);

		return response.isSuccess();
	}

	/**
	 * 签约自动扣款接口 先签约 - 再代扣
	 *
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2021/11/12 9:43
	 */
	@SneakyThrows
	public static String userAgreementPageSign(String agreementNo, String notifyUrl, String singleAmount) {

		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);

		AlipayUserAgreementPageSignRequest request = new AlipayUserAgreementPageSignRequest();

		AlipayUserAgreementPageSignModel signModel = new AlipayUserAgreementPageSignModel();

		//销售产品码，商户签约的支付宝合同所对应的产品码。 周期扣款场景固定为 CYCLE_PAY_AUTH。
		signModel.setProductCode("CYCLE_PAY_AUTH");
		//个人签约产品码，商户和支付宝签约时确定，商户可咨询技术支持。 周期扣款个人签约产品码固定为 CYCLE_PAY_AUTH_P
		signModel.setPersonalProductCode("CYCLE_PAY_AUTH_P");

		AccessParams accessParams = new AccessParams();
		accessParams.setChannel("ALIPAYAPP");
		signModel.setAccessParams(accessParams);
		//协议生效类型：DIRECT: 立即生效. NOTICE: 商户通知生效,
		signModel.setAgreementEffectType("DIRECT");
		//协议生效有效期：12个月
		signModel.setSignValidityPeriod("12m");
		//商户签约号，代扣协议中标示用户的唯一签约号（确保在商户系统中唯一）。
		signModel.setExternalAgreementNo(agreementNo);
		signModel.setSignScene("INDUSTRY|CARRENTAL");
		//TODO 周期扣款规则参数。使用周期扣款产品时需要传入，限定了发起扣款的周期、金额等。
		PeriodRuleParams periodRuleParams = new PeriodRuleParams();
		//扣款单位 DAY-即扣款周期按天计 MONTH-代表扣款周期按自然月。
		periodRuleParams.setPeriodType("MONTH");
		//扣款周期
		periodRuleParams.setPeriod(1L);
		//总扣款次数。如果传入此参数，则商户成功扣款的次数不能超过此次数限制（扣款失败不计入）。
		periodRuleParams.setTotalPayments(12L);
		//商户发起首次扣款的时间
		periodRuleParams.setExecuteTime(LocalDate.now().toString());
		//单次扣款最大金额
		periodRuleParams.setSingleAmount(singleAmount);
		//总金额限制
		periodRuleParams.setTotalAmount("1000");
		signModel.setPeriodRuleParams(periodRuleParams);
		//平台商户
		signModel.setThirdPartyType("PARTNER");
		request.setBizModel(signModel);

		//签约设置的回调地址
		request.setNotifyUrl(notifyUrl);

		AlipayUserAgreementPageSignResponse response = alipayClient.pageExecute(request);
		System.out.println(response.getBody());
		return response.getBody();

	}

	/**
	 * 签约自动扣款接口 代扣后签约
	 *
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2021/11/12 9:43
	 */
	@SneakyThrows
	public static String tradPaySign(String notifyUrl, String orderNo, String title, String agreementNo, String totalAmount) {

		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);

		AlipayTradePayRequest request = new AlipayTradePayRequest();

		AlipayTradePayModel payModel = new AlipayTradePayModel();
		//订单号
		payModel.setOutTradeNo(orderNo);
		//订单总金额。 单位为元，精确到小数点后两位
		payModel.setTotalAmount(totalAmount);
		//标题
		payModel.setSubject(title);
		//商家和支付宝签约的产品码
		payModel.setProductCode("CYCLE_PAY_AUTH");

		//定义代扣信息
		AgreementParams agreementParams = new AgreementParams();
		//协议号
		agreementParams.setAgreementNo(agreementNo);
		payModel.setAgreementParams(agreementParams);
		//设置回调地址
		request.setNotifyUrl(notifyUrl);
		request.setBizModel(payModel);

		AlipayTradePayResponse response = alipayClient.execute(request);

		System.out.println(response.getBody());
		if (response.isSuccess()) {
			System.out.println("调用成功");
			return response.getBody();
		} else {
			System.out.println("调用失败");
			return null;
		}
	}

	/**
	 * 查询支付宝自动续费协议接口
	 *
	 * @param agreementNo 协议号
	 * @return java.lang.String 协议信息
	 * @author nianhua.jiang
	 * @date 2021/11/15 16:43
	 */
	@SneakyThrows
	public static boolean agreementQuery(String agreementNo) {

		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
				AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);

		AlipayUserAgreementQueryRequest request = new AlipayUserAgreementQueryRequest();

		AlipayUserAgreementQueryModel queryModel = new AlipayUserAgreementQueryModel();

		queryModel.setPersonalProductCode("GENERAL_WITHHOLDING_P");
		queryModel.setSignScene("INDUSTRY|MEDICAL");
		queryModel.setAgreementNo(agreementNo);
		queryModel.setThirdPartyType("PARTNER");

		request.setBizModel(queryModel);

		AlipayUserAgreementQueryResponse response = alipayClient.execute(request);

		logger.info("协议号 " + agreementNo + " 的自动续费查询结果为 ==============>" + response.getBody());

		if (response.isSuccess()) {
			System.out.println("调用成功");
			return true;
		} else {
			System.out.println("调用失败");
			return false;
		}

	}


	@SneakyThrows
	public static void main(String[] args) {
		//System.out.println( tradPaySign("https://a99zg89a.xiaomy.net/member/pay/agreementSignNotify", ORDERNO, "自动续费", "20215924790191322337"));
		//System.out.println(userAgreementPageSign(AGREEMENTNO, "https://a99zg89a.xiaomy.net/member/pay/agreementSignNotify"));
		//System.out.println(agreementQuery("20215924790044217337"));
		//System.out.println(LocalDate.now().toString());
	}

}